<template>
    <div>
        <a-spin :spinning="spinningLoading">
            <div class="tips">
            </div>

        </a-spin>
    </div>
</template>
<script>
import ssoConfig from '@/config/sso';
export default {
    data() {
        return {
            spinningLoading: true,
        }
    },
    mounted() {
        let code = this.$route.query.code;
        let state = this.$route.query.state;
        // 获取code
        if (code) {
            console.log('code==', code);

            this.login(code);
        } else {
            this.getCode();
        }
    },
    methods: {
        getCode() {
            let origin = window.location.origin;
            let pathname = window.location.pathname;
            window.location.href = `${ssoConfig.url}?client_id=${ssoConfig.client_id}&redirect_uri=${origin + pathname}&response_type=code&state=0`
        },
        login(code) {
            this.getAxios(this.$iamSso, {
                code,
                scope: 'all',
                grant_type: 'iam',
                module_name: 'wit'
            },
                // module_name ?{"module_name": module_name} :''
            )
                .then((res) => {
                    console.log("接口信息返回", res);
                    const that = this;
                    if (res.code == 200) {
                        localStorage.baoxin = "imaSso";
                        that.$store.commit("SET_USER_INFO", res.data.snId);
                        that.$store.commit("SET_USER_TOKEN", res.data.token);
                        localStorage.setItem('SET_USER_INFO', res.data.snId)
                        localStorage.setItem('SET_USER_TOKEN', res.data.token)
                        // that.$router.push("/home/dashbord");
                        let redirect_uri = localStorage.getItem('redirect_uri');
                        if (redirect_uri && !redirect_uri.includes('iamSso') && !redirect_uri.includes('login')) {
                            window.location.href = redirect_uri;
                            localStorage.removeItem('redirect_uri');
                        } else {
                            this.$router.replace({ path: '/screen' });
                        }
                        // that.$router.push("/home/enterpriseInfo/list");
                        // that.spinningLoading = false;
                    }
                })
                .catch((err) => {
                    let that = this;
                    this.$error({
                        title: "提示",
                        content: err.response.data.error_description,
                        onOk() {
                            localStorage.removeItem("token");
                            localStorage.removeItem("refreshToken");
                            that.$router.push("/login");
                        }
                    });
                    this.spinningLoading = false;
                })
                .finally(() => {
                    // this.spinningLoading = false;
                });
        }
    }
}
</script>
<style>
.tips {
    width: 100%;
    height: 100vh;
}
</style>